/**
 * Created by wuhuan on 15/12/14.
 */
// 所有模块都通过 define 来定义
define(function (require, exports, module) {

    var Model = require('/plugins/extend/Model');

	$("#getEmailCode").on("click",function(){
		if(!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|)+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d)|(([a-z]|\d)([a-z]|\d|-|\.|_|~)*([a-z]|\d)))\.)+(([a-z])|(([a-z])([a-z]|\d|-|\.|_|~)*([a-z])))$/i.test($("#email").val())){
			layer.alert("请输入正确的邮箱地址");
			return;
		}
		var _that = this;
		$.get('/app/user/bindEmailCode.html?email='+$("#email").val(), function(data) {
			if(data.result){
				get_code_time(_that);
				layer.alert("发送成功");
			}else{
				layer.alert("发送失败，请稍后再试");
			}
		});
	});
	
	//修改邮箱地址，缺少API
	$("#getEmailCodeEdit").on("click",function(){
		
		var _that = this;
		$.get('', function(data) {
			if(data.result){
				get_code_time(_that);
				layer.alert("发送成功");
			}else{
				layer.alert("发送失败，请稍后再试");
			}
		});
	});
	
    $("#bindingEmailForm").validate({
        rules: {
            'email': {
                required: true,
                email:true
            },
            'code': {
                required: true,
                rangelength:[6,6] 
            },
        },
        messages: {
            'email': {
                required: "请输入邮箱",
                email: "邮箱格式不正确"
            },
            'code': {
                required: "请输入邮箱验证码",
                rangelength: $.validator.format("邮箱验证码必须是6位."),
            }
        },
        submitHandler: function (form, event, validator) {
            $(form).ajaxSubmit(function (data) {
                if(!Model.showMsg(data)){
                    return;
                }else{
                    window.location.href = "/mobile/usercenter/security/setting.html";
                }
            });
        }
    });
    
    //修改邮箱form验证
    $("#editingEmailForm").validate({
        rules: {
            'code': {
                required: true,
                rangelength:[6,6] 
            },
        },
        messages: {
            'code': {
                required: "请输入邮箱验证码",
                rangelength: $.validator.format("邮箱验证码必须是6位."),
            }
        },
        submitHandler: function (form, event, validator) {
            $(form).ajaxSubmit(function (data) {
                if(!Model.showMsg(data)){
                    return;
                }else{
                    $(form).hide();
                    $("#bindingEmailForm").show();
                }
            });
        }
    });
   
    
    
    var wait = 60;
	get_code_time = function (elem) {
		if (wait == 0) {
			$(elem).text("重新发送");
			$(elem).next().hide();
			$(elem).show();
			wait = 60;
		} else {
			$(elem).next().text("("+wait+")秒后重新获取");
			$(elem).hide();
			$(elem).next().show();
			wait--;
			setTimeout(function() {
				get_code_time(elem);
			}, 1000);
		}
	};

});